﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using Model;
using BLL;

namespace WebUI.SystemManage
{
    public partial class Users : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bn_add.OnClientClick = window1.GetShowReference("UsersEdit.aspx?action=new", "新增用户");
                BindGrid();
            }
            if (Request[Common.LinkService.RefreshTag] == Common.LinkService.RefreshString)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            Grid1.DataSource = UsersManage.GetList().Tables[0];
            Grid1.DataBind();
        }
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            StringBuilder strWhere = new StringBuilder();
            if(tb_name.Text.Trim() != "" || tb_sid.Text.Trim() != "" || tb_username.Text.Trim() != "" || rbl_role.SelectedValue != "-1")
            {
                if (tb_name.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" name LIKE '%{0}%'",tb_name.Text.Trim());
                }
                if (tb_sid.Text.Trim() != "")
                {
                    if (tb_name.Text.Trim() != "") 
                    {
                        strWhere.Append(" AND");
                    }
                    strWhere.AppendFormat(" sid LIKE '%{0}%'",tb_sid.Text.Trim());
                }
                if (tb_username.Text.Trim() != "")
                {
                    if (tb_name.Text.Trim() != "" || tb_sid.Text.Trim() != "")
                    {
                        strWhere.Append(" AND");
                    }
                    strWhere.AppendFormat(" username LIKE '%{0}%'",tb_username.Text.Trim());
                }
                if (rbl_role.SelectedValue != "-1")
                {
                    if (tb_name.Text.Trim() != "" || tb_sid.Text.Trim() != "" || tb_username.Text.Trim() != "")
                    {
                        strWhere.Append(" AND");
                    }
                    strWhere.AppendFormat(" isTeacher = {0}",rbl_role.SelectedValue);
                }
                System.Threading.Thread.Sleep(500);
                Grid1.DataSource = UsersManage.GetList(strWhere.ToString()).Tables[0];
                Grid1.DataBind();
            }
            else
            {
                Alert.Show("请输入查询条件");
            }
        }
        protected void bnReset_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(600);
            tb_name.Text = "";
            tb_sid.Text = "";
            tb_username.Text = "";
            rbl_role.SelectedValue = "-1";
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
                UsersManage.Delete(id);
                BindGrid();
            }
        }
        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }
    }
}